/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fgv.tcc.fgv.imovel.service;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

/**
 *
 * @author adao
 */
@Path("envioDeArquivos")
public class FileResource {

    private static final String BASE_PATH =  System.getProperty("java.io.tmpdir")+"/";
            
    @POST
    @Path("uploadArquivo")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
    public String uploadArquivo(
            @FormDataParam("arquivo") InputStream fileInputStream,
            @FormDataParam("arquivoNome") String fileDisposition)
            throws FileNotFoundException, IOException {

        String fileName = System.currentTimeMillis()+"_"+ fileDisposition;
        System.out.println("***** fileName " + fileDisposition);
        String filePath = BASE_PATH + fileName;
        try (OutputStream fileOutputStream = new FileOutputStream(filePath)) {
            int read = 0;
            final byte[] bytes = new byte[1024];
            while ((read = fileInputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, read);
            }
        }

        return "ok";
    }
    
    

}
